import { handleActions } from 'redux-actions'
import { success } from '../ui-components'
import util from '../util'

const init = {
  userIdentifyProfile: {}, // 所有部门目标
  sidebars: [],
  avatar: {},
	profile: {},
	atList: {},
	addresses: {},
  org: {},
  rolePermissons: [],
  myProfile: {},
  // step guide step
  guide: 1,
  naviStep: null,
  prevNaviStep: null,
}
export default handleActions({

  'freeze guide': (state, { payload }) => {
    const { prevNaviStep, naviStep } = state;
    // unfreeze
    if (!payload) {
      if (naviStep) return state;
      return {
        ...state,
        naviStep: state.prevNaviStep,
      };
    }
    return {
      ...state,
      prevNaviStep: state.naviStep,
      naviStep: null,
    };
  },

  'get guide': (state, {payload}) => {
    return {
      ...state,
      naviStep: payload.naviStep,
    }
  },

  'update guide': (state, { payload }) => ({
    ...state,
    naviStep: payload,
    guide: 1,
  }),

  'set step guide': (state, { payload }) => ({
    ...state,
    guide: payload,
  }),

  'clear next guide': (state) => ({
    ...state,
    nextGuide: false,
  }),

  'next guide': (state) => ({
    ...state,
    nextGuide: true,
  }),

  'show guide': (state) => ({
    ...state,
    hasGuide: true,
  }),

  'clear guide': (state) => ({
    ...state,
    org: {
      ...state.org,
      isNaviOrg: false,
    },
    guide: 1,
    naviStep: null,
    prevNaviStep: null,
  }),

  'fetch my profile': (state, { payload }) => ({
    ...state,
    myProfile: payload
  }),

  "fetch user Identify profile": (state, {payload}) => {
    return {
      ...state,
      userIdentifyProfile: payload
    }
  },

  'fetch team director list': (state, { payload }) => {
    const { keyword, data } = payload
    return {
      ...state,
      atList: {
        ...state.atList,
        [keyword]: data
      }
    }
  },

	'fetch org': (state, {payload}) => ({
		...state,
		org: payload
	}),

  'reset org': (state) => ({
    ...state,
    rolePermissons: []
  }),

	'sync org': (state, { payload }) => ({
		...state,
		org: {
			...state.org,
			...payload
		}
	}),

	'fetch role permissions': (state, { payload }) => ({
		...state,
		rolePermissons: payload
	}),

	'sync role permissions': (state, { payload }) => {
		const { resourceName, scopeName, roleName } = payload
		return {
			...state,
			rolePermissons: state.rolePermissons.map(item => {
				if (item.resourceName == resourceName && item.roleName == roleName) {
					if (item.scopeName === scopeName) {
						return {
							...item,
							default: true
						}
					}
					return {
						...item,
						default: false
					}
				}
				return item
			})
		}
	},

  'fetch profile': (state, {payload}) => {
    return {
      ...state,
      profile: payload
    }
  },

  'fetch live notification list': (state, {payload}) => {
    return {
      ...state,
      liveNotifications: payload
    }
  },

  'fetch unread number': (state, {payload}) => {
    return {
      ...state,
      unread: payload
    }
  },

	'fetch at list': (state, {payload}) => {
    const { keyword, data } = payload;
    return {
      ...state,
      atList: {
        ...state.atList,
        [keyword]: data // 将搜索的关键字与返回的结果组成key - value结构
      }
    }
  },

  'clear at list': (state, action) => ({
    ...state,
    atList: {}
  }),

	'fetch addresses': (state, { payload }) => {
		const { addressRegionVOs, parentId } = payload
		return {
			...state,
			addresses: {
				...state.addresses,
				[parentId]: addressRegionVOs
			}
		}
	},

  'update avatar': (state, { payload }) => ({
    ...state,
    avatar: payload
  }),

  /*
  'save user profile': (state, { payload }) => {
    const { callback, ...others } = payload
    success('保存成功!')
    callback && callback()
    return {
      ...state
    }
  },
  */

  'fetch sidebar': (state, { payload }) => ({
    ...state,
    sidebars: payload
  })
}, init)
